package com.regent.central.bill.web.mrp.purchase;

import com.regent.central.bill.core.mrp.BillMaterialPurchase;
import com.regent.central.bill.repository.mrp.dto.*;
import com.regent.central.bill.repository.mrp.dto.arrive.PurchaseMaterialRemittanceDTO;
import com.regent.central.bill.service.RemittanceService;
import com.regent.central.bill.service.mrp.BillMaterialPurchaseService;
import com.regent.central.bill.web.mrp.BillMaterialPurchaseOrderNoListResponse;
import com.regent.central.bill.web.mrp.BillMaterialPurchaseResponse;
import com.regent.central.bill.web.mrp.RemittanceUndoResponse;
import com.regent.central.infrastructure.web.BasicResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.http.MediaType;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Set;

/**
 * 新建采购单Controller
 *
 * @author 钟宝林
 * @date 2018-06-16 14:51
 **/
@RestController
@RequestMapping(value = "/bill/material/purchase", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class BillMaterialPurchaseController {

    @InitBinder
    private void initBinder(WebDataBinder binder) {
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"), true));
        binder.registerCustomEditor(Date.class, new CustomDateEditor(new SimpleDateFormat("yyyy-MM-dd"), true));
    }

    @Resource
    private RemittanceService remittanceService;

    @Resource
    private BillMaterialPurchaseService billMaterialPurchaseService;

    @PutMapping("/manual-arrive-complete")
    public BasicResponse manualArriveComplete(@RequestParam("billMaterialPurchaseId") String billMaterialPurchaseId,
                                              @RequestParam("facMissionId") String facMissionId,
                                              @RequestParam("materialSkuId") String materialSkuId) {
        if (StringUtils.isBlank(billMaterialPurchaseId) || StringUtils.isBlank(facMissionId) || StringUtils.isBlank(materialSkuId)) {
            return new BasicResponse(400, "参数不全");
        }

        billMaterialPurchaseService.manualArriveComplete(billMaterialPurchaseId, facMissionId, materialSkuId);
        return new BasicResponse();
    }

    @PostMapping("/add")
    public BasicResponse create(@RequestBody MaterialPurchaseAddDTO purchaseAddDTO) {
        if (purchaseAddDTO == null) {
            return new BasicResponse(400, "参数不全");
        }
        Set<MaterialPurchaseAddDTO.PurchaseAddMaterialsSkuDTO> materialsSkus = purchaseAddDTO.getMaterialsSkus();
        if (materialsSkus == null || materialsSkus.size() <= 0) {
            return new BasicResponse(400, "参数不全");
        }
        billMaterialPurchaseService.create(purchaseAddDTO);
        return new BasicResponse();
    }

    @GetMapping
    public BillMaterialPurchasePageDTO queryBillPurchase(BillMaterialPurchaseQueryDTO billMaterialPurchaseQueryDTO) {
        return billMaterialPurchaseService.listMaterialPurchase(billMaterialPurchaseQueryDTO, billMaterialPurchaseQueryDTO.getPageNum(), billMaterialPurchaseQueryDTO.getPageSize());
    }

    @GetMapping("/{billMaterialPurchaseId:\\w+}")
    public BillMaterialPurchaseResponse detail(@PathVariable("billMaterialPurchaseId") String billMaterialPurchaseId) {
        BillMaterialPurchaseResponse response = new BillMaterialPurchaseResponse();
        BillMaterialPurchaseDetailDTO purchaseDetail = billMaterialPurchaseService.getBillPurchase(billMaterialPurchaseId);
        response.setDetail(purchaseDetail);
        return response;
    }

    @GetMapping("/order-no")
    public BillMaterialPurchaseOrderNoListResponse listPurchaseOrderNo(@RequestParam("unitId") String unitId) {
        BillMaterialPurchaseOrderNoListResponse response = new BillMaterialPurchaseOrderNoListResponse();
        List<String> orderNos = billMaterialPurchaseService.listOrderNoByUnitId(unitId);
        response.setOrderNos(orderNos);
        return response;
    }

    @PostMapping("/remit")
    public BasicResponse remit(@RequestBody PurchaseMaterialRemittanceDTO materialRemittanceDTO) {
        billMaterialPurchaseService.remit(materialRemittanceDTO);
        return new BasicResponse();
    }

    /**
     * 流水撤销
     */
    @DeleteMapping(value = "/remit/undo")
    public RemittanceUndoResponse remitUndo(RemittanceUndoDTO remittanceUndoDTO) {
        remittanceService.cancel(remittanceUndoDTO.getRemittanceId(), remittanceUndoDTO.getUserId());
        return new RemittanceUndoResponse();
    }

    @GetMapping("/undo/{billMaterialPurchaseId:\\w+}")
    public BasicResponse undo(@PathVariable("billMaterialPurchaseId") String billMaterialPurchaseId) {
        BasicResponse response = new BasicResponse();
        PurchaseUndoResult result = billMaterialPurchaseService.undo(billMaterialPurchaseId);
        if(!result.isSuccess()){
            response.setCode(1);
        }
        return response;
    }

    @GetMapping("/keep-arrive-status")
    public BasicResponse keepArriveStatus() {
        List<BillMaterialPurchase> billMaterialPurchases = billMaterialPurchaseService.selectListAll();
        for (BillMaterialPurchase billMaterialPurchase : billMaterialPurchases) {
            billMaterialPurchaseService.keepBillPurchaseArriveStatus(billMaterialPurchase.getId());
        }
        return new BasicResponse();
    }

}
